﻿
 /*******************************************************************
 ***  you will need a copy of the following FIMCM binaries:       ***
 ***  Microsoft.Clm.Shared                                        ***
 ***  Add those as reference to the project                       ***
 *******************************************************************/

 /*******************************************************************
 *** installation:
 *** copy the assenly to the FIMCM bin & the fimcm web bin folder ***
 *** configure the events you are interested in to use this 
 *** handler 
 *******************************************************************/
namespace FimCmTools.NotificationHandlers
{
    using System;
    using System.Diagnostics;
    using System.Reflection;
    using Microsoft.Clm.Shared;
    using Microsoft.Clm.Shared.Requests;

    /// <summary>
    /// Diagnostic tracer
    /// </summary>
    public class LoggingHandler : INotificationSink
    {
        private static string _name;
        /// <summary>
        /// Name for tracing
        /// </summary>
        public string Name
        {
            get { return _name; }
            private set { _name = value; }
        }

        /// <summary>
        /// Initialize handler
        /// </summary>
        /// <param name="data"></param>
        public void Initialize(string data)
        {
            Name = string.IsNullOrEmpty(data) ? AppDomain.CurrentDomain.BaseDirectory : data;
            Assembly a = Assembly.GetExecutingAssembly();
            AssemblyName n = a.GetName();


            Trace.WriteLine(
                string.Format("LoggingHandler:[{0}] v{1} created. RT:'{2}', {3}",
                    Name,
                    n.Version,
                    a.ImageRuntimeVersion,
                    n.ProcessorArchitecture
                    )
                    );
        }

        /// <summary>
        /// Notification handler function
        /// </summary>
        /// <param name="notification"></param>
        public void Notify(Microsoft.Clm.Shared.Notifications.Notification notification)
        {
            if (notification != null)
            {
                if (notification.Request != null)
                {
                    Trace.WriteLine(string.Format("LoggingHandler:[{0}] Type {1}/{2} Status {3}. RequestID {4}, User {5} DataCollectionFlags {6} ",
                        Name,
                        notification.NotificationType,
                        notification.Request.RequestType,
                        notification.Request.Status,
                        notification.Request.Uuid,
                        notification.Request.TargetUserUuid,
                        notification.Request.DataCollectionFlags)
                        );
                    if (notification.Request.DataCollection != null)
                    {
                        Trace.WriteLine(string.Format(
                            "LoggingHandler:[{0}]{1} items",
                             Name,
                    notification.Request.DataCollection.Count));
                        foreach (DataCollectionItem item in notification.Request.DataCollection)
                        {
                            Trace.WriteLine(string.Format(
                                "LoggingHandler:[{0}][{1}][{2}]",
                                  Name,
                                  string.IsNullOrEmpty(item.Name) ? "No Name" : item.Name,
                                  string.IsNullOrEmpty(item.Value) ? "No Value" : item.Value
                                ));
                        }
                    }
                }
                else
                {
                    Trace.WriteLine(string.Format("LoggingHandler:[{0}] Type {1}, Success {2}",
                        Name,
                        notification.NotificationType,
                        notification.IsSuccess));
                }
            }
            else
            {
                Trace.WriteLine("received a null notification");
            }
        }
    }
}
